package cn.kgc.service;

import cn.kgc.dao.BillMapper;
import cn.kgc.dao.ProviderMapper;
import cn.kgc.entity.Provider;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator on 2020/3/10.
 */
@Service
public class ProviderServiceImpl implements ProviderService {

    @Autowired
    private ProviderMapper providerMapper;

    @Autowired
    private BillMapper billMapper;

    public PageInfo<Provider> queryByCodeAndName(Provider provider, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Provider> list = providerMapper.queryByCodeAndName(provider);
        return new PageInfo<Provider>(list);
    }

    public Provider queryById(Integer id) {
        return providerMapper.queryById(id);
    }

    public Integer delete(Integer id) {
        //先删除订单信息
        billMapper.deleteByProviderId(id);
        //之后再删除供应商信息
        Integer count = providerMapper.delete(id);
        return count;
    }

    public List<Provider> queryByCode(String code) {
        return providerMapper.queryByCode(code);
    }

    public List<Provider> queryAll() {
        //不传值代表查询所有
        Provider provider = new Provider();
        return providerMapper.queryByCodeAndName(provider);
    }

    public Integer save(Provider provider) {
        return providerMapper.save(provider);
    }

    public Integer update(Provider provider) {
        return providerMapper.update(provider);
    }
}
